package com.zhao.retry.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhao.retry.demo.Param;
import com.zhao.retry.demo.Retry;
import com.zhao.retry.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created on 2020/5/20.
 *
 * @author zhao
 * @email 136689260@qq.com
 */
@Retry
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {

    private AtomicInteger cnt = new AtomicInteger(0);

    @Retry
    @Override
    public String service(String param) {
        log.info("service {} ", param);
        if (cnt.get() <= 5) {
            throw new RuntimeException("mock");
        } else {
            return param;
        }
    }

    @Override
    public String service(String[] param) {
        return service(JSON.toJSONString(param));
    }

    @Override
    public String service(List param) {
        return service(JSON.toJSONString(param));
    }

    @Override
    public String service(Map param) {
        return service(JSON.toJSONString(param));
    }

    @Override
    public String service(Param param) {
        return service(JSON.toJSONString(param));
    }

    @Recover
    @Override
    public void recover(RuntimeException e) {
        log.info("recover {} ", e.getMessage());
        log.error("recover  ", e);
    }
}
